package ru.mcfr.oxygen.framework.operations;

import org.xml.sax.InputSource;
import ro.sync.ecss.extensions.api.*;
import ro.sync.ecss.extensions.api.access.AuthorXMLUtilAccess;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import java.awt.*;
import java.io.File;
import java.io.StringWriter;
import java.net.MalformedURLException;

/**
 * Custom transform operation using Saxon EE transformer.
 */
public class TrasformOperation implements AuthorOperation {

    public void doOperation(AuthorAccess authorAccess, ArgumentsMap arguments)
            throws IllegalArgumentException, AuthorOperationException {

        // Choose the XSLT used in the transform operation
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setMultiSelectionEnabled(false);
        // Accepts only the XSLT files.
        fileChooser.setFileFilter(new FileFilter() {

            public String getDescription() {
                return "XSLT files";
            }


            public boolean accept(File f) {
                String fileName = f.getName();
                return f.isFile() && (fileName.endsWith(".xsl") || fileName.endsWith(".xslt"));
            }
        });
        if (fileChooser.showOpenDialog((Component) authorAccess.getWorkspaceAccess().getParentFrame()) == JFileChooser.APPROVE_OPTION) {
            try {
                String xslSystemId = fileChooser.getSelectedFile().toURL().toString();
                // Create the XSLT source used in transform operation
                Source xslSource = new SAXSource(new InputSource(xslSystemId));
                Transformer xslTransformer = authorAccess.getXMLUtilAccess().createXSLTTransformer(xslSource, null, AuthorXMLUtilAccess.TRANSFORMER_SAXON_ENTERPRISE_EDITION);
                Source editorSource = new SAXSource(new InputSource(authorAccess.getEditorAccess().createContentReader()));
                StringWriter result = new StringWriter();
                // Transform the current document using the specified XSLT
                xslTransformer.transform(editorSource, new StreamResult(result));
                StringBuffer resultBuffer = result.getBuffer();
                // Display the result
                authorAccess.getWorkspaceAccess().showInformationMessage("Transformation result: " + resultBuffer.toString());
                authorAccess.getWorkspaceAccess().showStatusMessage("Transformation finished");
            } catch (TransformerConfigurationException e) {
                // Display the error message
                authorAccess.getWorkspaceAccess().showErrorMessage(e.getMessage());
            } catch (TransformerException e) {
                // Display the error message
                authorAccess.getWorkspaceAccess().showErrorMessage(e.getMessage());
            } catch (MalformedURLException e) {
                // Display the error message
                authorAccess.getWorkspaceAccess().showErrorMessage(e.getMessage());
            }
        }
    }

    /**
     * Arguments
     */
    public ArgumentDescriptor[] getArguments() {
        return null;
    }

    /**
     * Description
     */
    public String getDescription() {
        return "Custom transform operation using Saxon EE transformer.";
    }
}
